﻿using System;
using System.Windows.Forms;
using UniMana.Utils.Data;
using System.Data;
using UniMana.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using UniMana.Win.Training;
using UniMana.Utils.Helper;
using UniMana.Win;

namespace UniMana.Win.Training
{
    public partial class FrmStudent_Class_SubFilter : DevExpress.XtraEditors.XtraForm
    {
        DBMain m_data = new DBMain();
        private int _Flag = 1;
        private int m_iClassKey = 0;
        int m_iDepartmentKey = 0;
        int m_iLevelKey = 0;
        int m_iCourseKey = 0;
        int m_iTrainingTypeKey = 0;
        int m_iMajorKey = 0;

        private DataTable m_tblClass;

        public FrmStudent_Class_SubFilter()
        {
            InitializeComponent();
            this.CenterToScreen();  // Canh giữa màn hình sau khi thay đổi kích thước form
            m_iDepartmentKey = Utils.LoginInfo.GblLoginInfo.iDepartmentKey;
        }

        private void FrmStudent_Class_Filter_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// Lấy dữ liệu
        /// </summary>

        private void LoadData()
        {
            lueCatalogue_Level.GetData(m_data.m_DCReadOnly.Catalogue_Levels);
            lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable());
            lueCatalogue_Course.GetData(m_data.m_DCReadOnly.Catalogue_Courses);
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.GetDepartmentManageStudent().ToDataTable());

            if (m_iDepartmentKey > 0)
                lueCatalogue_Department.EditValue = m_iDepartmentKey;

            gluCatalogue_Major.GetData(m_data.m_DCReadOnly.Catalogue_Majors.GetListByDepartmentKey(m_data.m_DCReadOnly.Catalogue_Levels,
                m_iDepartmentKey).ToDataTable());

            DataTable m_tblPerson = new DataTable();
            m_tblPerson = m_data.m_DCReadOnly.Personnel_Personnels.ToDataTable();
            if (m_tblPerson.Rows.Count > 0)
            {
                for (int i = 0; i < m_tblPerson.Rows.Count; i++)
                {
                    try
                    {
                        m_tblPerson.Rows[i]["Name"] = m_tblPerson.Rows[i]["LastName"] + " " + m_tblPerson.Rows[i]["Name"];
                    }
                    catch { continue; }
                }
                rluPersonnel_Personnel.GetData(m_tblPerson, "Personnel_PersonnelKey", "Name");
            }
        }

        /// <summary>
        /// Lấy thông tin khoa, khóa, ngành, bậc đào tạo, loại hình đào tạo
        /// </summary>

        private void GetData()
        {
            try { m_iCourseKey = int.Parse(lueCatalogue_Course.EditValue.ToString()); }
            catch { m_iCourseKey = 0; }
            try { m_iDepartmentKey = int.Parse(lueCatalogue_Department.EditValue.ToString()); }
            catch { m_iDepartmentKey = 0; }
            try { m_iLevelKey = int.Parse(lueCatalogue_Level.EditValue.ToString()); }
            catch { m_iLevelKey = 0; }
            try { m_iMajorKey = int.Parse(gluCatalogue_Major.EditValue.ToString()); }
            catch { m_iMajorKey = 0; }
            try { m_iTrainingTypeKey = int.Parse(lueCatalogue_TrainingType.EditValue.ToString()); }
            catch { m_iTrainingTypeKey = 0; }
        }

        /// <summary>
        /// Giới hạn lại dữ liệu với quyền được phép
        /// </summary>
        /// <param name="m_tblTable"></param>
        /// <returns></returns>

        private DataTable Limit(DataTable m_tblTable)
        {
            DataTable m_tbl = new DataTable();
            m_tbl = m_tblTable;
            for (int i = 0; i < m_tbl.Rows.Count; i++)
            {
                int d = 0;
                for (int j = 0; j < Utils.LoginInfo.GblLoginInfo.iListDepartment.Count; j++)
                {
                    if (m_tbl.Rows[i]["Catalogue_DepartmentKey"].ToString() == Utils.LoginInfo.GblLoginInfo.iListDepartment[j].ToString())
                    {
                        d = 1;
                        break;
                    }
                }
                if (d == 0)
                {
                    m_tbl.Rows.RemoveAt(i);
                    i--;
                }
            }
            return m_tbl;
        }

        /// <summary>
        /// Lấy danh sách lớp với các điều kiện
        /// </summary>

        private void LoadClass()
        {
            try
            {
                if (ckeByID.Checked)
                {
                    m_tblClass = new DataTable();
                    m_tblClass = m_data.m_DCReadOnly.Training_Classes.GetListClassWithID(txtID.Text,
                        m_iDepartmentKey,
                        m_iMajorKey,
                        m_iLevelKey,
                        m_iTrainingTypeKey,
                        m_iCourseKey).ToDataTable();
                    m_tblClass = Limit(m_tblClass);
                }
                if (ckeByName.Checked)
                {
                    m_tblClass = new DataTable();
                    m_tblClass = m_data.m_DCReadOnly.Training_Classes.GetListClassWithName(txtID.Text,
                        m_iDepartmentKey,
                        m_iMajorKey,
                        m_iLevelKey,
                        m_iTrainingTypeKey,
                        m_iCourseKey).ToDataTable();
                    m_tblClass = Limit(m_tblClass);
                }

                // Kiểm tra dữ liệu
                if (m_tblClass.Rows.Count != 0)
                    gctClass.DataSource = m_tblClass;
                else
                {
                    gctClass.DataSource = null;
                    XtraMessageBox.Show("Không tìm thấy lớp");
                }
            }
            catch { }
        }

        /// <summary>
        /// Sự kiện enter tìm lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GetData();
                LoadClass();
            }
        }

        /// <summary>
        /// Xem danh sách lớp với các điều kiện
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnView_Click(object sender, EventArgs e)
        {
            GetData();
            LoadClass();
        }

        /// <summary>
        /// Xem danh sách sinh viên của lớp chọn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gctClass_DoubleClick(object sender, EventArgs e)
        {
            if ((!grvClass.IsFilterRow(grvClass.FocusedRowHandle)) && (!grvClass.IsGroupRow(grvClass.FocusedRowHandle)))
            {
                m_iClassKey = int.Parse(grvClass.GetFocusedRowCellValue("Training_ClassKey").ToString());
                FrmStudent_Class stdcls = new FrmStudent_Class(m_iClassKey);
                try
                {
                    foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                    {
                        if (p.Text == stdcls.Text)
                        {
                            ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                            break;
                        }
                    }
                }
                catch { }
                TabShowFrm.ShowTabbedMdi(stdcls, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                this.Close();
            }
        }

        /// <summary>
        /// Thay đổi khoa
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (_Flag == 1)
                {
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) <= 0)
                    {
                        // Nạp lại dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học

                        lueCatalogue_Level.EditValue = null;
                        lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        gluCatalogue_Major.GetData((this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, m_data.m_DCReadOnly.Catalogue_Levels)).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeInTraingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseKeyNameInTrainingProgram(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors)).ToDataTable(), noChoose: true);
                    }
                    else
                    {
                        // Lấy dữ liệu cho bậc đào tạo, ngành, loại hình đào tạo, khóa học theo khoa

                        lueCatalogue_Level.EditValue = null;
                        this.lueCatalogue_Level.GetData((this.m_data.m_DCReadOnly.Catalogue_Levels.GetLevelByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        gluCatalogue_Major.EditValue = null;
                        this.gluCatalogue_Major.GetData((this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms,m_data.m_DCReadOnly.Catalogue_Levels, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), "Catalogue_MajorKey", noChoose: true);

                        lueCatalogue_TrainingType.EditValue = null;
                        this.lueCatalogue_TrainingType.GetData((this.m_data.m_DCReadOnly.Catalogue_TrainingTypes.GetTrainingTypeByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);

                        lueCatalogue_Course.EditValue = null;
                        this.lueCatalogue_Course.GetData((this.m_data.m_DCReadOnly.Catalogue_Courses.GetCourseByDepartment(this.m_data.m_DCReadOnly.Training_TrainingPrograms, this.m_data.m_DCReadOnly.Catalogue_Majors, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue))).ToDataTable(), noChoose: true);
                    }
                }
            }
            catch { }
        }

        /// <summary>
        /// Thay đổi ngành
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gluCatalogue_Major_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue) > 0)
                {
                    _Flag = 0;
                    lueCatalogue_Department.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetDepartmentKeyByMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_DepartmentKey"];
                    _Flag = 1;

                    lueCatalogue_Level.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetLevelKeyByDepartmentMajor(this.m_data.m_DCReadOnly.Training_TrainingPrograms, GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue), GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue))).ToDataTable().Rows[0]["Catalogue_LevelKey"];
                }
            }
            catch { }
        }

        /// <summary>
        /// Thay đổi bậc đào tạo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lueCatalogue_Level_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue) > 0)
                    if (GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue) > 0)
                        gluCatalogue_Major.EditValue = (this.m_data.m_DCReadOnly.Catalogue_Majors.GetMajorKeyByDepartmentLevel(this.m_data.m_DCReadOnly.Training_TrainingPrograms,
                            GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue),
                            GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue))).ToDataTable().Rows[0]["Catalogue_MajorKey"];
            }
            catch { }
        }

        /// <summary>
        /// Thêm lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmStudent_Class_Add clsadd = new FrmStudent_Class_Add(GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Department.EditValue),
                GetValueLookUpEdit.ConvertValueToInt(gluCatalogue_Major.EditValue),
                GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Level.EditValue),
                GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_Course.EditValue),
                GetValueLookUpEdit.ConvertValueToInt(lueCatalogue_TrainingType.EditValue));
            clsadd.ShowDialog();
        }

        /// <summary>
        /// Xóa lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvClass.SelectedRowsCount > 0)
                {
                    if (m_data.m_DCReadOnly.Training_Student_Classes.ReturnStudentNumberInClass(m_iClassKey) > 0)
                        XtraMessageBox.Show("Lớp " + grvClass.GetFocusedRowCellValue("ID").ToString() + " có " + m_data.m_DCReadOnly.Training_Student_Classes.ReturnStudentNumberInClass(m_iClassKey) + " sinh viên. Không thể xóa", "Thông báo");
                    else
                    {
                        DialogResult result = XtraMessageBox.Show("Bạn có muốn xóa lớp " + grvClass.GetFocusedRowCellValue("ID").ToString(), "Thông báo", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            Training_ClassInfo cinfo = new Training_ClassInfo(m_iClassKey);
                            cinfo.Delete();
                            LoadClass();
                        }
                    }
                }
                else
                    XtraMessageBox.Show("Chưa chọn lớp", "Thông báo");
            }
            catch { }
        }

        /// <summary>
        /// Chỉnh sửa lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvClass.SelectedRowsCount > 0)
                {
                    FrmStudent_Class_Edit FrmEdit = new FrmStudent_Class_Edit(m_iClassKey);
                    FrmEdit.ShowDialog();
                    LoadClass();
                }
                else
                    XtraMessageBox.Show("Chưa chọn lớp", "Thông báo");
            }
            catch { }
        }

        /// <summary>
        /// Chọn 1 lớp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void gctClass_Click(object sender, EventArgs e)
        {
            try
            {
                m_iClassKey = int.Parse(grvClass.GetFocusedRowCellValue("Training_ClassKey").ToString());
            }
            catch { }
        }
    }
}